<?php
defined('BASEPATH') or exit('No direct script access allowed!');

class MComment extends CI_Model {
  function __construct() {
    parent::__construct();
    $this->load->database();
    $this->load->library('elfSecurity');
  }

  function get($hash) {
    return $this->db->where('taskHash', $hash)->get('comment')->result();
  }

  function add($taskId, $taskHash, $projectHash, $author, $content) {
    do {
      $hash = $this->elfsecurity->randStr(15);
      $result = $this->db->where('hash', $hash)->get('comment')->row();
    } while (!empty($result));
    $this->db->insert('comment', array(
      'hash' => $hash,
      'taskId' => $taskId,
      'taskHash' => $taskHash,
      'projectHash' => $projectHash,
      'author' => $author,
      'content' => $content
    ));
    return $this->getSingle($taskHash, $hash);
  }

  function getSingle($taskHash, $hash) {
    return $this->db->where('taskHash', $taskHash)->where('hash', $hash)->get('comment')->row();
  }

  function delete($hash) {
    return $this->db->where('hash', $hash)->delete('comment');
  }
}
